package com.flyox.game.fivetiger.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.flyox.game.fivetiger.bean.CONSTS;
import com.flyox.game.fivetiger.bean.ChessOptions;
import com.flyox.game.fivetiger.control.FivetigerControl;

public class ChessOptionsJDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = -8774315225609181939L;
	private JPanel whoFirstPanel;
	private ButtonGroup whoFirstGroup;
	private JRadioButton clientFirstRadioButton;
	private JRadioButton ServerFirstRadioButton;

	private JRadioButton shapeCountRadioButton;
	private JRadioButton oneRadioButton;
	private ButtonGroup addRemoveCountGroup;
	private JPanel addRemoveCountPanel;

	private JRadioButton removeShapeNoRadioButton;
	private JRadioButton removeShapeYesRadioButton;
	private ButtonGroup removeShapeGroup;
	private JPanel removeShapePanel;

	private JPanel controlPanel;
	private JButton OKButton;

	private ChessOptions chessOptions ;
	private boolean editAble = true;
	private FivetigerControl control;
	
	public ChessOptionsJDialog(FivetigerControl control,final ChessOptions chessOptions,boolean editAble) {
		super(control.getView(),true);
		this.control=control;
		this.chessOptions=(ChessOptions) chessOptions.clone();
		this.editAble=editAble;
		initGUI();
		initContent();
	}

	private void initContent() {
		//
		if (chessOptions.getWhoFirst().equals(CONSTS.SYS_SERVER_NAME)) {
			ServerFirstRadioButton.setSelected(true);
		} else {
			clientFirstRadioButton.setSelected(true);
		}
		//
		if (chessOptions.isRemoveShape()) {
			removeShapeYesRadioButton.setSelected(true);
		} else {
			removeShapeNoRadioButton.setSelected(true);
		}
		//
		if (chessOptions.getSteptAddRemoveCount() == 0) {
			shapeCountRadioButton.setSelected(true);
		} else {
			oneRadioButton.setSelected(true);
		}
		//
		if (isEditAble()) {
			ServerFirstRadioButton.setEnabled(true);
			clientFirstRadioButton.setEnabled(true);
			removeShapeYesRadioButton.setEnabled(false);
			removeShapeNoRadioButton.setEnabled(false);
			shapeCountRadioButton.setEnabled(false);
			oneRadioButton.setEnabled(false);
		} else {
			ServerFirstRadioButton.setEnabled(false);
			clientFirstRadioButton.setEnabled(false);
			removeShapeYesRadioButton.setEnabled(false);
			removeShapeNoRadioButton.setEnabled(false);
			shapeCountRadioButton.setEnabled(false);
			oneRadioButton.setEnabled(false);
		}
	}

	private void initGUI() {
		try {
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					saveOptions();
					dispose();
				}
			});

			GridLayout thisLayout = new GridLayout(2, 2);
			thisLayout.setColumns(2);
			thisLayout.setHgap(5);
			thisLayout.setVgap(5);
			thisLayout.setRows(2);
			getContentPane().setLayout(thisLayout);
			getContentPane().setBackground(new java.awt.Color(191,202,230));
			{
				whoFirstPanel = new JPanel();
				BoxLayout whoFirstPanelLayout = new BoxLayout(whoFirstPanel, javax.swing.BoxLayout.Y_AXIS);
				whoFirstPanel.setLayout(whoFirstPanelLayout);
				getContentPane().add(whoFirstPanel);
				whoFirstPanel.setToolTipText("\u8c01\u5148\u8d70");
				whoFirstPanel.setName("\u8c01\u5148\u8d70");
				whoFirstPanel.setBorder(BorderFactory.createTitledBorder("\u8c01\u5148\u8d70"));
				whoFirstPanel.setPreferredSize(new java.awt.Dimension(91, 75));
				whoFirstPanel.setBackground(new java.awt.Color(115,136,193));
				{
					ServerFirstRadioButton = new JRadioButton();
					whoFirstPanel.add(ServerFirstRadioButton);
					ServerFirstRadioButton.setText("\u521b\u5efa\u8005");
				}
				{
					clientFirstRadioButton = new JRadioButton();
					whoFirstPanel.add(clientFirstRadioButton);
					clientFirstRadioButton.setText("\u52a0\u5165\u8005");
				}
				whoFirstGroup = new ButtonGroup();
				whoFirstGroup.add(ServerFirstRadioButton);
				ServerFirstRadioButton.setBackground(new java.awt.Color(115,136,193));
				whoFirstGroup.add(clientFirstRadioButton);
				clientFirstRadioButton.setBackground(new java.awt.Color(115,136,193));

			}
			{
				removeShapePanel = new JPanel();
				BoxLayout removeShapePanelLayout = new BoxLayout(removeShapePanel, javax.swing.BoxLayout.Y_AXIS);
				removeShapePanel.setLayout(removeShapePanelLayout);
				getContentPane().add(removeShapePanel);
				removeShapePanel.setBorder(BorderFactory.createTitledBorder("\u62d4\u56fe\u5f62\u68cb"));
				removeShapePanel.setBackground(new java.awt.Color(115,136,193));
				{
					removeShapeYesRadioButton = new JRadioButton();
					removeShapePanel.add(removeShapeYesRadioButton);
					removeShapeYesRadioButton.setText("\u53ef\u4ee5");
				}
				{
					removeShapeNoRadioButton = new JRadioButton();
					removeShapePanel.add(removeShapeNoRadioButton);
					removeShapeNoRadioButton.setText("\u4e0d\u53ef\u4ee5");
				}
				removeShapeGroup = new ButtonGroup();
				removeShapeGroup.add(removeShapeYesRadioButton);
				removeShapeYesRadioButton.setBackground(new java.awt.Color(115,136,193));
				removeShapeGroup.add(removeShapeNoRadioButton);
				removeShapeNoRadioButton.setBackground(new java.awt.Color(115,136,193));
			}
			{
				addRemoveCountPanel = new JPanel();
				BoxLayout addRemoveCountPanelLayout = new BoxLayout(addRemoveCountPanel, javax.swing.BoxLayout.Y_AXIS);
				addRemoveCountPanel.setLayout(addRemoveCountPanelLayout);
				getContentPane().add(addRemoveCountPanel);
				addRemoveCountPanel.setBorder(BorderFactory.createTitledBorder("\u62d4\u68cb\u4e2a\u6570"));
				addRemoveCountPanel.setBackground(new java.awt.Color(115,136,193));
				{
					shapeCountRadioButton = new JRadioButton();
					addRemoveCountPanel.add(shapeCountRadioButton);
					shapeCountRadioButton.setText("\u8ba1\u7b97\u56fe\u5f62");
				}
				{
					oneRadioButton = new JRadioButton();
					addRemoveCountPanel.add(oneRadioButton);
					oneRadioButton.setText("\u4e00\u4e2a");
				}
				addRemoveCountGroup = new ButtonGroup();
				addRemoveCountGroup.add(shapeCountRadioButton);
				shapeCountRadioButton.setBackground(new java.awt.Color(115,136,193));
				addRemoveCountGroup.add(oneRadioButton);
				oneRadioButton.setBackground(new java.awt.Color(115,136,193));
			}
			{
				controlPanel = new JPanel();
				getContentPane().add(controlPanel);
				controlPanel.setLayout(null);
				controlPanel.setBackground(new java.awt.Color(115,136,193));
				{
					OKButton = new JButton();
					controlPanel.add(OKButton);
					OKButton.setText("\u786e\u5b9a");
					OKButton.setBounds(12, 32, 65, 22);
					OKButton.setBackground(new java.awt.Color(252,245,76));
					OKButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							OKButtonActionPerformed(evt);
						}
					});
				}
			}
			this.getRootPane().setDefaultButton(OKButton);
			this.setSize(200, 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return 
	 * true changed
	 * false same
	 */
	private void saveOptions() {
		if (ServerFirstRadioButton.isSelected()) {
			chessOptions.setWhoFirst(CONSTS.SYS_SERVER_NAME);
		} else {
			chessOptions.setWhoFirst(CONSTS.SYS_CLIENT_NAME);
		}
		if (removeShapeYesRadioButton.isSelected()) {
			chessOptions.setRemoveShape(true);
		} else {
			chessOptions.setRemoveShape(false);
		}
		if (shapeCountRadioButton.isSelected()) {
			chessOptions.setSteptAddRemoveCount(0);
		} else {
			chessOptions.setSteptAddRemoveCount(1);
		}
		
		//save to doc
		control.getDoc().setChessOptions(chessOptions);
	}

	private void OKButtonActionPerformed(ActionEvent evt) {
		saveOptions();
		this.dispose();
	}

	public boolean isEditAble() {
		return editAble;
	}

}
